<?php
/**
 * Menu block
 * @author Kirill A Egorov 2011 DVelum project
 */
class Block_Menu extends Block_Abstract
{
	protected $_template = 'menu.php';
	protected $_tree;
	const CACHE_KEY = 'block_menu_data';
	
	protected function _collectData()
	{
		$cache = Application::dataCache();
		if($cache && $data = $cache->load(self::CACHE_KEY)){
			$this->_tree = unserialize($data);
			return;
		}
		
		$this->_tree = new Tree();
		$data = Model_Page::getInstance()->getListVc(
			array(
				'sort'=>'order_no',
				'direction'=>'DESC'
			),
			array(
				'published'=>1,
				'in_menu_block'=>1
			),
			false,
			array('id','code','menu_title','parent_id' , 'order_no')
		);
		
		if(!empty($data))
			foreach ($data as $item)
				$this->_tree->addItem($item['id'], $item['parent_id'], $item , $item['order_no']);
		
		if($cache)
			$cache->save(serialize($this->_tree) , self::CACHE_KEY);
		
	}
	/**
	 * (non-PHPdoc)
	 * @see Block_Abstract::render()
	 */
	public function render()
	{
		$this->_collectData();
		$tpl = new Template();
		$tpl->data = $this->_config;
		$tpl->tree = $this->_tree;
		$tpl->lang = $this->_lang;
		return $tpl->render(Application::getTemplatesPath() . $this->_template);
	}
}